package config

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
	"miomp-terminal-suite/backend/config"
	"miomp-terminal-suite/backend/global"
	"path/filepath"
)

func Init() {
	global.CONF = new(config.AppConfig)
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(filepath.Join("./", "miomp"))

	err := viper.ReadInConfig()
	if err != nil {
		fmt.Printf("viper.ReadInConfig() faild, err: %v\n", err)
	}

	err = viper.Unmarshal(global.CONF)
	if err != nil {
		fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
	}

	viper.WatchConfig()
	viper.OnConfigChange(func(in fsnotify.Event) {
		fmt.Println("配置文件修改了")
		err = viper.Unmarshal(global.CONF)
		if err != nil {
			fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
		}
	})
	// 把读取到的配置信息反序列化到Conf变量中
}
